添加jar包到hive hive> add jar /home/hadoop/data/hive/hive_UDF.jar;
创建临时函数 语法:
1 2 3
CREATE TEMPORARY FUNCTION function_name AS class_name; function_name函数名 class_name 类路径,包名+类名
实例: 创建sayhello函数
1 2 3 4 5 6 7 8 9 10 11
hive> create temporary function sayhello as 'com.kun.hive.HelloUDF'; OK Time taken: 0.485 seconds hive> hive> show functions; 【查看可以看到sayhello】 OK rpad rtrim sayhello second sentences
测试:
1 2 3 4 5 6 7 8 9
hive> select sayhello('wuwang'); OK Hello:wuwang Time taken: 2.712 seconds, Fetched: 1 row(s) hive> select sayhello('wuwang','nihao'); OK Hello:wuwang=>nihao Time taken: 0.128 seconds, Fetched: 1 row(s) hive>
删除临时函数 :
语法:
DROP TEMPORARY FUNCTION [IF EXISTS] function_name;
实例测试:
1 2 3 4 5 6
hive> DROP TEMPORARY FUNCTION IF EXISTS sayhello; OK Time taken: 0.003 seconds hive> select sayhello('wuwang'); FAILED: SemanticException [Error 10011]: Line 1:7 Invalid function 'sayhello' hive>
创建永久函数
上传语法:
1 2 3
CREATE FUNCTION [db_name.]function_name AS class_name [USING JAR|FILE|ARCHIVE 'file_uri' [, JAR|FILE|ARCHIVE 'file_uri'] ]; file_uri:是hdfs上的jar包目录
CREATE TEMPORARY FUNCTION function_name AS class_name USING JAR path; function_name函数名 class_name 类路径,包名+类名 path jar包hdfs路径
创建实例:
1 2 3 4 5 6 7 8 9 10 11 12
hive> CREATE FUNCTION sayhello2 AS 'com.kun.hive.HelloUDF' USING JAR 'hdfs://hadoop:8020/lib/hive_UDF.jar' > ; converting to local hdfs://hadoop:8020/lib/hive_UDF.jar Added [/tmp/4b56b71a-d406-4ee7-89db-9d3c97a19e81_resources/hive_UDF.jar] to class path Added resources: [hdfs://hadoop:8020/lib/hive_UDF.jar] OK Time taken: 0.552 seconds hive> show functions;【查看】 OK dayofmonth decode default.sayhello2
测试:
1 2 3 4
hive> select sayhello2('wuwang'); OK Hello:wuwang Time taken: 1.228 seconds, Fetched: 1 row(s)
hive> select sayhello2('wu'); converting to local hdfs://hadoop:8020/lib/hive_UDF.jar Added [/tmp/9dcbba84-d9db-4bcf-8eed-2cf20abfc510_resources/hive_UDF.jar] to class path Added resources: [hdfs://hadoop:8020/lib/hive_UDF.jar] OK Hello:wu Time taken: 2.477 seconds, Fetched: 1 row(s) hive>